/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.wicket.pages;

import com.googlecode.jsimplemarket.model.NTipRecurenta;
import com.googlecode.jsimplemarket.model.NTipTaxa;
import com.googlecode.jsimplemarket.service.GenericService;
import java.util.List;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Alexandru.Codescu
 */
public class TipTaxaEditPage extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<NTipTaxa> service;
    @SpringBean(name = "genericService")
    private GenericService<NTipRecurenta> serviceRecurenta;
    
    private final NTipTaxa entity;
    
    public TipTaxaEditPage() {
        entity = new NTipTaxa();
        init();
    }
    
    public TipTaxaEditPage(Long id) {
        entity = service.getById(NTipTaxa.class, id);
        init();
    }
    
    private void init() {
        Form<NTipTaxa> form = new Form<NTipTaxa>("form");
        form.setModel(new CompoundPropertyModel<NTipTaxa>(entity));
        
        TextField<String> tfCod = new TextField<String>("cod");
        tfCod.setRequired(true);
        form.add(tfCod);
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        TextField<String> tfUnitateMasura = new TextField<String>("unitateMasura");
        tfUnitateMasura.setRequired(true);
        form.add(tfUnitateMasura);
        
        DropDownChoice<NTipRecurenta> ddcTipRecurenta = new DropDownChoice<NTipRecurenta>("tipRecurenta", new LoadableDetachableModel<List<NTipRecurenta>>() {

            @Override
            protected List<NTipRecurenta> load() {
                return serviceRecurenta.getList(NTipRecurenta.class, null, null);
            }
            
        }, new ChoiceRenderer<NTipRecurenta>("denumire", "id"));
        ddcTipRecurenta.setRequired(true);
        form.add(ddcTipRecurenta);
        
        DateTextField dtfDataInceput = new DateTextField("dataInceput");
        dtfDataInceput.add(new DatePicker());
        form.add(dtfDataInceput);
        
        DateTextField dtfDataSfarsit = new DateTextField("dataSfarsit");
        dtfDataSfarsit.add(new DatePicker());
        form.add(dtfDataSfarsit);
        
        form.add(new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (entity.getId() == null) {
                    service.insert(entity);
                } else {
                    service.update(entity);
                }
                setResponsePage(new NomenclatorViewPage(NTipTaxa.class, TipTaxaEditPage.class));
            }
            
        });
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(new NomenclatorViewPage(NTipTaxa.class, TipTaxaEditPage.class));
            }
            
        });
        
        add(form);
    }
}
